package com.dragon.www.haier.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dragon.www.haier.R;

import org.dragonnova.lamp.DeviceController;
import org.dragonnova.lamp.Tap;
import org.dragonnova.lamp.utils.HexUtil;

import java.util.List;

/**
 * HarerLampAdapter
 * Created by haojinyu on 10月27 027.
 */
public class HaierTapAdapter extends BaseAdapter {

    private Context mContext;
    private List<Tap> mTaps;
    private DeviceController mDeviceController;


    public HaierTapAdapter(Context context, List<Tap> list, DeviceController controller) {
        mContext = context;
        mTaps = list;
        mDeviceController = controller;
    }

    @Override
    public int getCount() {
        return mTaps.size();
    }

    @Override
    public Object getItem(int position) {
        return mTaps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Tap tap = (Tap) getItem(position);
        final TabViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new HaierTapAdapter.TabViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.tab_view_item, parent, false);
            viewHolder.tabbutton = (ImageView) convertView.findViewById(R.id.tabbutton);
            viewHolder.tv_tab_desc = (TextView) convertView.findViewById(R.id.tv_tab_desc);
            viewHolder.tab_layout = (LinearLayout) convertView.findViewById(R.id.tab_layout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (TabViewHolder) convertView.getTag();
        }

        if (tap.getRefreshStatus() == Tap.ON) {
            viewHolder.tabbutton.setActivated(true);
            viewHolder.tv_tab_desc.setText("阀门" + String.format("%s", HexUtil.bytesToString(new byte[]{(byte) tap.getId()})) + "已开启");

        } else if (tap.getRefreshStatus() == Tap.OFF) {
            viewHolder.tabbutton.setActivated(false);
            viewHolder.tv_tab_desc.setText("阀门" + String.format("%s", HexUtil.bytesToString(new byte[]{(byte) tap.getId()})) + "已关闭");
        }

        return convertView;
    }


    private class TabViewHolder extends AppHolder {
        LinearLayout tab_layout;
        ImageView tabbutton;
        TextView tv_tab_desc;
    }

}
